# -*- coding: UTF-8 -*-

from .service.common import Common
from .service.model import ModelService

from jsonrpc.env import Environment


class RPC(object):
    def __init__(self, base_url, timeout, dbname):
        self.base_url = base_url
        self.timeout = timeout
        self.dbname = dbname
        self.env = Environment(self)
        self.addons_models = {}

    def init_addons_models(self, addons_models_list):
        for addons_models in addons_models_list:
            self.addons_models.update(addons_models)

    def login(self, username, password):
        common = Common(self.base_url, self.timeout)
        uid = common.login(self.dbname, username, password)
        self.username = username
        self.uid = uid
        self.password = password
        return uid

    def call(self, model, method, *args):
        base_url = self.base_url
        timeout = self.timeout
        dbname = self.dbname
        username = self.username
        password = self.password
        uid = self.uid
        rpc = ModelService(base_url, timeout, dbname, username, password, uid)
        return rpc.execute(model, method, *args)

    def call_kw(self, model, method, args, kwargs):
        base_url = self.base_url
        timeout = self.timeout
        dbname = self.dbname
        username = self.username
        password = self.password
        uid = self.uid
        rpc = ModelService(base_url, timeout, dbname, username, password, uid)
        return rpc.execute_kw(model, method, args, kwargs)
